<?php

namespace plugin\ycMassage\utils\sms;

use app\common\manager\SettingsMgr;
use Exception;
use GuzzleHttp\Client;

class Smsbao
{
    public static function main($saas_appid, $mobile, $code)
    {
        $config = SettingsMgr::getChildren($saas_appid, 'sms');
        if (empty($config['smsbao_username']) || empty($config['smsbao_password'])) {
            throw new Exception("请先配置短信宝");
        }
        $smsbao_sign = $config['smsbao_sign'];
        $smsbao_username = $config['smsbao_username'];
        $smsbao_password = $config['smsbao_password'];
        $content = urlencode("【{$smsbao_sign}】您的验证码是{$code}。如非本人操作，请忽略本短信");
        $client = new Client();
        $req = $client->get("https://api.smsbao.com/sms?u={$smsbao_username}&p={$smsbao_password}&m={$mobile}&c={$content}");
        $res = $req->getBody()->getContents();
        $statusStr = [
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        ];
        if ($res == 0) {
            return true;
        } else {
            throw new \Exception("短信发送失败：" . $statusStr[$res]);
        }
    }
    public static function send($saas_appid, $mobile, $message)
    {
        $config = SettingsMgr::getChildren($saas_appid, 'sms');
        if (empty($config['smsbao_username']) || empty($config['smsbao_password'])) {
            throw new Exception("请先配置短信宝");
        }
        $smsbao_sign = $config['smsbao_sign'];
        $smsbao_username = $config['smsbao_username'];
        $smsbao_password = $config['smsbao_password'];
        $content = urlencode("【{$smsbao_sign}】{$message}");
        $client = new Client();
        $req = $client->get("https://api.smsbao.com/sms?u={$smsbao_username}&p={$smsbao_password}&m={$mobile}&c={$content}");
        $res = $req->getBody()->getContents();
        $statusStr = [
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        ];
        if ($res == 0) {
            return true;
        } else {
            throw new \Exception("短信发送失败：" . $statusStr[$res]);
        }
    }
}
